package com.istock.user.controller.vue;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.istock.base.common.api.model.ApiBaseResponse;
import com.istock.base.common.api.model.RetStatusEnum;
import com.istock.base.serializer.json.TypeReference;
import com.istock.union.user.client.SSOClientUtils;
import com.istock.union.user.model.Constants;
import com.istock.union.user.model.DictDataInfo;
import com.istock.union.user.model.DictTypeData;
import com.istock.union.user.model.SecurityChecker;
import com.istock.union.user.service.DictInfoService;

@Controller
@RequestMapping("/s/dictInfo")
public class VueDictInfoController {

	@Autowired
	private DictInfoService dictInfoService;
	
	private static String EDIT_DICT_CACHE_ID = "EDIT_DICT_ID";
	
	@RequestMapping("initEditCache")
    @SecurityChecker("user:dictInfo:edit")
    public @ResponseBody
    ApiBaseResponse<Object> initEditCache(String id) {
        DictTypeData cacheObject;
        if (StringUtils.isEmpty(id)) {
            cacheObject = new DictTypeData();
        } else {
            cacheObject = dictInfoService.findTypeById(id);
        }
        SSOClientUtils.getInstance().putObject(EDIT_DICT_CACHE_ID, cacheObject);
        return new ApiBaseResponse<Object>(RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE, 
                Constants.API_RESPONSE_SUCCESS_MESSAGE , null);
    }
	
	@RequestMapping("getDictTypeData")
    @SecurityChecker("user:dictInfo:edit")
    @ResponseBody
    public ApiBaseResponse<DictTypeData> getDictTypeData(String id) {
        if (StringUtils.isBlank(id)) {
            return new ApiBaseResponse<DictTypeData>(RetStatusEnum.FAIL,Constants.API_RESPONSE_FAIL_CODE, 
                    "type id is null",null);
        }

        DictTypeData dictTypeData = dictInfoService.findTypeById(id);
        SSOClientUtils.getInstance().putObject(EDIT_DICT_CACHE_ID, dictTypeData);
        return new ApiBaseResponse<DictTypeData>(RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE, 
                Constants.API_RESPONSE_SUCCESS_MESSAGE, dictTypeData);
    }
	
	@RequestMapping("getDictDataInfo")
    @SecurityChecker("user:dictInfo:edit")
    @ResponseBody
    public ApiBaseResponse<DictDataInfo> getDictDataInfo(String id) {
        if (StringUtils.isBlank(id)) {
            return new ApiBaseResponse<DictDataInfo>(RetStatusEnum.FAIL,Constants.API_RESPONSE_FAIL_CODE, 
                    "data id is null",null);
        }

        DictTypeData cacheObject = SSOClientUtils.getInstance().getObject(EDIT_DICT_CACHE_ID, new TypeReference<DictTypeData>() {});
        for (DictDataInfo dataInfo : cacheObject.getDataList()) {
            if (dataInfo.getId().equalsIgnoreCase(id)) {
                return new ApiBaseResponse<DictDataInfo>(RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE, 
                        Constants.API_RESPONSE_SUCCESS_MESSAGE, dataInfo);
            }
        }
        return new ApiBaseResponse<DictDataInfo>(RetStatusEnum.FAIL,Constants.API_RESPONSE_FAIL_CODE, 
                "data is null",null);
    }
}
